﻿using UnityEngine;
using System.Collections;

public class BattleCamera : MonoBehaviour {

	public bool bCamAtkFocus;

	//원호 공격 포커스//
	bool isSupportAtkcameraFocus;
	bool isCaster;
	public GameObject middleCam;
	public Transform cameraRootTransform;
	public Transform mainCamTransform;
	Transform targetManifestTransform;
	PawnManifest targetManifest;
	Transform meTransform;

	bool bStartedCoroutine;
	Vector3 targetPos;


	public void SupportCamFocus(bool _isCaster, PawnManifest _target, PawnManifest _me)
	{
		if(BattleManager.Instance.bOverdriveMode)
			return;
		if(!BattleSetting.Instance.useSupportAtkCamFocus)
			return;
		if(isSupportAtkcameraFocus)
			return;

		bCamAtkFocus=true;
		isSupportAtkcameraFocus=true;
		isCaster = _isCaster;
		targetManifest = _target;
		targetManifestTransform = _target.transform;
		meTransform = _me.transform;
	}

	public void CamAtkFocus(bool _bActivate, PawnManifest _target)
	{
		if(!BattleSetting.Instance.useAttackCamFocus)
			return;
		isSupportAtkcameraFocus=false;
		bCamAtkFocus=_bActivate;

		if(_bActivate) {
			targetManifest = _target;
			targetManifestTransform = _target.transform;
			targetPos = GetCamAtkTargetPos();

			if(!bStartedCoroutine) {
				bStartedCoroutine=true;
				StartCoroutine("CoCamAtkFocus");
			}
		}
	}

	public void StopCamAtkFocus()
	{
		if(bCamAtkFocus) {
			Camera.main.gameObject.transform.localRotation = Quaternion.identity;	
			bStartedCoroutine=false;
			StopCoroutine("CoCamAtkFocus");
			bCamAtkFocus = false;
		}
	}

	public void ClearCamAtkFocus()
	{
		bStartedCoroutine=false;
		StopCoroutine("CoCamAtkFocus");
		bCamAtkFocus = false;
	}
	
	IEnumerator CoCamAtkFocus()
	{
		while(true)
		{
			float camFocusSpeed = BattleSetting.Instance.attackCamFocusSpeed;
			if(isSupportAtkcameraFocus && BattleSetting.Instance.useSupportAtkCamFocus) {
				camFocusSpeed = BattleSetting.Instance.supportCamFocusSpeed;
				if(isCaster)
					targetPos = GetCamAtkTargetPos();
				else {//적과 나의 중간 지점을 바라보도록//
					targetPos = (targetManifestTransform.position + meTransform.position) * 0.5f;
					targetPos.y += 1;
				}
			}
			Quaternion targetRot = Quaternion.LookRotation(targetPos - cameraRootTransform.position);

			if(bCamAtkFocus && BattleSetting.Instance.useAttackCamFocus) {
				if(targetRot.IsValid()) {
					if( ( Camera.main != null ) && ( Camera.main.gameObject != null ) && ( Camera.main.gameObject.activeSelf == true ) )
						mainCamTransform.rotation = Quaternion.Slerp(mainCamTransform.rotation, targetRot, camFocusSpeed * Time.deltaTime);
					else
						Debug.LogError("  ========================  camera  null =================");
				}
			}
			
			else {
				if( ( Camera.main != null ) && ( Camera.main.gameObject != null ) && ( Camera.main.gameObject.activeSelf == true ) )
					mainCamTransform.localRotation = Quaternion.Lerp(mainCamTransform.localRotation, Quaternion.identity, BattleSetting.Instance.attackCamFocusSpeed * 0.5f * Time.deltaTime);
				else
					Debug.LogError("  ========================  camera  null =================");
			}

			yield return null;
		}
		
	}

	//타겟 앞 일정거리 떨어진 지점//
	Vector3 GetCamAtkTargetPos()
	{
		Vector3 targetPos = targetManifestTransform.position;
		targetPos = targetPos +   targetManifest.originalRot * Vector3.forward * BattleSetting.Instance.attackCamTargetOffset;
		targetPos.y += 1;
		return targetPos;
	}

	//원호 공격 2타부터 카메라 전진//
	public void SupportAtkCamForward(int nCount)
	{
		Vector3 vDir = mainCamTransform.forward;
		Vector3 _original = mainCamTransform.position;
		
		Vector3 _dest = _original + vDir * BattleSetting.Instance.supportAtkCamForwardOffset;

		if(!BattleSetting.Instance.useAttackCamFocus)
			return;
		LeanTween.move( Camera.main.gameObject, _dest, 0.1f).setEase(LeanTweenType.easeOutCirc);
		if(nCount== BattlePawn.totalSupportAtkCount)
			LeanTween.moveLocal( Camera.main.gameObject, Vector3.zero, BattleSetting.Instance.supportAtkCamBackInterpolaTime).setDelay(0.1f);
	}

	public void Init()
	{
		cameraRootTransform = BattleManager.Instance.cameraRoot.transform;
		middleCam = cameraRootTransform.FindChild("Middle").gameObject;
		mainCamTransform = Camera.main.gameObject.transform;

		EventlineStaticActor.Camera = BattleManager.Instance.cameraRoot;
		if(middleCam!=null)
			EventlineStaticActor.MiddleCam = middleCam;
		EventlineStaticActor.MainCamera = Camera.main.gameObject;

		EventlineStaticActor.RealTimeLight = BattleManager.Instance.defauntBG.transform.FindChild("Directional light (Real Time)").gameObject;
		EventlineStaticActor.RealTimeLight.SetActive(true);

		if(BattleManager.Instance.isBossBattle)
		{
			AudioListener aListener = Camera.main.gameObject.GetComponent<AudioListener>();
			if(aListener!=null)
				Destroy(aListener);
			GameObject goListener = new GameObject();
			goListener.name = "audioListener";
			goListener.AddComponent<AudioListener>();
			goListener.transform.parent = mainCamTransform;
			BattleUtil.ClearTransformLocal(goListener.transform);
			goListener.transform.position = goListener.transform.position + BattleManager.Instance.cameraRoot.transform.forward * BattleSetting.Instance.bossBattleAudioListenerAhead;
		}
	}

//	public void StartIdleCamera()
//	{
//	}

	public void ShowWaveCam(int nWave, bool isPvp)
	{
		CamTakeSetting.Instance.PlayIntroCam(nWave, isPvp);
	}

}
